package com.cjst.controller.base;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjst.controller.base.BaseController;
import com.cjst.pojo.sys.SysUser;
import com.cjst.sdp.util.MenuNode;
import com.cjst.service.sys.SysMenuService;
import com.cjst.service.sys.SysUserService;

/**
 * 登录相关
 *
 * @author yj
 */
@Controller
public class LoginController extends BaseController {

    private static final Logger log = Logger.getLogger(LoginController.class);

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 首页
     */
    @RequestMapping(value = "/login/blackboard", method = RequestMethod.GET)
    public String blackboard() {
        return "blackboard";
    }

    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {
        return "login";
    }


    @RequestMapping("/login")
    public String login(HttpServletRequest request, Map<String, Object> map) throws Exception {
    	log.info("HomeController.login()");
        // 登录失败从request中获取shiro处理的异常信息。
        // shiroLoginFailure:就是shiro异常类的全类名.
        String exception = (String) request.getAttribute("shiroLoginFailure");
        log.info("exception=" + exception);
        String msg = "";
        if (exception != null) {
            if (UnknownAccountException.class.getName().equals(exception)) {
            	log.info("UnknownAccountException -- > 账号不存在：");
                msg = "UnknownAccountException -- > 账号不存在：";
            } else if (IncorrectCredentialsException.class.getName().equals(exception)) {
            	log.info("IncorrectCredentialsException -- > 密码不正确：");
                msg = "IncorrectCredentialsException -- > 密码不正确：";
            } else if ("kaptchaValidateFailed".equals(exception)) {
            	log.info("kaptchaValidateFailed -- > 验证码错误");
                msg = "kaptchaValidateFailed -- > 验证码错误";
            } else {
                msg = "else >> " + exception;
                System.out.println("else -- >" + exception);
            }
        }
        map.put("msg", msg);
        // 此方法不处理登录成功,由shiro进行处理
        return "/login";
    }

    /**
     * 跳转首页
     */
    @RequestMapping({"/", "/index"})
    public String index(Model model) {
        //获取菜单列表
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        List<MenuNode> munuList = sysMenuService.menuRight(sysUser,0,null);
        List<MenuNode> titles = MenuNode.buildTitle(munuList);
        model.addAttribute("userName", sysUser.getUserName());
        model.addAttribute("titles", titles);
        return "index";
    }

    /**
     * 无权限页面
     */
    @RequestMapping("/403")
    public String unauthorizedRole() {
        System.out.println("------没有权限-------");
        return "403";
    }

    /**
     * 检查登陆状态
     */
    @RequestMapping("/login/checklogin")
    @ResponseBody
    public Object checklogin() {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser != null) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 首页
     */
    @RequestMapping("/notice/hello")
    @ResponseBody
    public Object hello() {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        return "shouye";
    }
}


